package com.mystore.console.controller;

import com.mystore.common.utils.security.Md5Utils;
import com.mystore.framework.pojo.User;
import com.mystore.framework.service.impl.IUserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("console/user")
public class UserCenterController {
    @Resource
    private IUserServiceImpl iUserServiceImpl;
    @GetMapping("/userCenter.do")
    public String userCenter(String userId, Model model,String msg){
        User user;
        if (!StringUtils.hasText(userId) ||(user=iUserServiceImpl.selectById(Integer.parseInt(userId)))==null){
            model.addAttribute("msg","请登录");
            return "console_login";
        }
        else{
            model.addAttribute("msg",msg);
            model.addAttribute("user",user);
            return "console_user_center";
        }
    }
    @PostMapping("save.do")
    public String save(User user, Model model){
        User myUser=iUserServiceImpl.selectById(user.getUserId());
        if(myUser==null){
            model.addAttribute("msg","用户不存在，修改失败！");
        }else {
            myUser.setUserNewPassword(user.getUserNewPassword());
            myUser.setUserName(user.getUserName());
            if(iUserServiceImpl.updateOne(myUser))
                model.addAttribute("msg","修改完成！");
            else
                model.addAttribute("msg","输入信息可能错误，修改失败！请联系服务器管理员");
        }
        return "user_update_result";
    }
    @GetMapping("updateUserPassword.do")
    public String updateUserPassword(int userId, RedirectAttributes attributes,Model model){
        if(iUserServiceImpl.selectById(userId)!=null){
            model.addAttribute("userId",userId);
            return "console_update_user_password";
        }
        else {
            attributes.addAttribute("msg","无法修改，请检查您是否登录");
            attributes.addAttribute("userId",userId);
            return "redirect:/console/user/userCenter.do";
        }
    }
    @PostMapping("saveUserPassword.do")
    public String saveUserPassword(User user, RedirectAttributes attributes,Model model){
        User myUser;
        if(user==null&&user.getUserId()==null)
            return "redirect:/";
        if((myUser=iUserServiceImpl.selectById(user.getUserId()))!=null){
            if(myUser.getUserPassword().equals(Md5Utils.getMd5(user.getUserPassword()))){
                myUser.setUserNewPassword(user.getUserNewPassword());
                if(iUserServiceImpl.updateOne(myUser)){
                    attributes.addAttribute("userId",user.getUserId());
                    attributes.addAttribute("msg","修改成功！");
                    return "redirect:/console/user/userCenter.do";
                }
            }
            model.addAttribute("userId",user.getUserId());
            model.addAttribute("msg","修改失败，你的密码不正确");
        }
        else {
            model.addAttribute("msg","修改失败，你的账户也许未登录");
            model.addAttribute("userId",user.getUserId());
        }
        return "console_update_user_password";
    }@GetMapping("updateUserName.do")
    public String updateUserName(int userId, RedirectAttributes attributes,Model model){
        User user;
        if((user=iUserServiceImpl.selectById(userId))!=null){
            model.addAttribute("userId",userId);
            model.addAttribute("userName",user.getUserName());
            return "console_update_user_name";
        }
        else {
            attributes.addAttribute("msg","无法修改，请检查您是否登录");
            attributes.addAttribute("userId",userId);
            return "redirect:/console/user/userCenter.do";
        }
    }
    @PostMapping("saveUserName.do")
    public String saveUserName(User user, RedirectAttributes attributes,Model model){

        if(user==null&&user.getUserId()==null)
            return "redirect:/";
        if(user.getUserName().length()>16){
            model.addAttribute("msg","修改失败，你的昵称过长");
            model.addAttribute("userId",user.getUserId());
            model.addAttribute("userName",iUserServiceImpl.selectById(user.getUserId()).getUserName());
            return "console_update_user_name";
        }
        User myUser;
        if((myUser=iUserServiceImpl.selectById(user.getUserId()))!=null){
            myUser.setUserName(user.getUserName());
            if(iUserServiceImpl.updateOne(myUser)){
                attributes.addAttribute("userId",user.getUserId());
                attributes.addAttribute("msg","修改成功！");
                return "redirect:/console/user/userCenter.do";
            }
        }
        else {
            model.addAttribute("msg","修改失败，你的账户也许未登录");
            model.addAttribute("userId",user.getUserId());
            model.addAttribute("userName",user.getUserName());
        }
        return "console_update_user_name";
    }
    @GetMapping("/logout.do")
    public String logout(Model model){
        if(iUserServiceImpl.logout())
            return "redirect:/";
        model.addAttribute("msg","退出失败");
        return "redirect:/";
    }
}
